define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'card/index' + location.search,
                    add_url: 'card/add',
                    edit_url: 'card/edit',
                    del_url: 'card/del',
                    multi_url: 'card/multi',
                    import_url: 'card/import',
                    table: 'card',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'status',
                search: false,
                searchFormVisible: true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'no', title: __('No')},
                        {field: 'tag', title: __('Tag')},
                        {field: 'status', title: __('Status'), searchList: Config.statusList, visible: false},
                        {field: 'status_text', title: __('Status'), operate: false, formatter: Table.api.formatter.status, custom:{'已使用': 'success', '已禁用': 'danger'}},
                        {field: 'agent_id', title: __('代理ID'), visible: false},
                        {field: 'agent.nickname', title: __('代理名称'), operate: 'LIKE'},
                        {field: 'use_time', title: __('Use_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'update_time', title: __('Update_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {
                            field: 'operate', 
                            title: __('Operate'), 
                            table: table, 
                            buttons: [
                                {
                                name: 'detail',
                                text: '使用详情',
                                title: '使用详情',
                                classname: 'btn btn-xs btn-info btn-dialog',
                                url: 'card/detail'
                            },
                            {
                                name: 'editinfo',
                                text: '修改',
                                title: '修改',
                                classname: 'btn btn-xs btn-danger btn-dialog',
                                url: 'card/active'
                            },
                             {
                                name: 'active',
                                text: '激活',
                                title: '激活',
                                classname: 'btn btn-xs btn-danger btn-dialog',
                                url: 'card/active'
                            },
                                {
                                name: 'agent',
                                text: '分配代理',
                                title: '分配代理',
                                classname: 'btn btn-xs btn-success btn-dialog',
                                url: 'card/agent'
                            }],
                            events: Table.api.events.operate, 
                            formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                var table = $(that.table).clone(true);

                                if(row.status == 1) {
                                    $(table).data("operate-agent", null);
                                } else {
                                    $(table).data("operate-detail", null);
                                     $(table).data("operate-editinfo", null);
                                }

                                if(row.status != 0) {
                                    $(table).data("operate-active", null);
                                    $(table).data("operate-edit", null);
                                }

                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        agent: function () {
            Controller.api.bindevent();
        },
        active: function () {
            Controller.api.bindevent();
        },
        editinfo: function () {
            Controller.api.bindevent();
        },
        detail: function () {
            Controller.api.bindevent();
        },
        batch: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});